package org.streets.extention.lucene;

import java.util.Map;

import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.ScopeConstants;
import org.apache.tapestry5.ioc.annotations.Scope;
import org.apache.tapestry5.ioc.services.PerthreadManager;
import org.apache.tapestry5.ioc.services.RegistryShutdownHub;

import org.slf4j.Logger;

import org.streets.extention.lucene.services.IndexSource;
import org.streets.extention.lucene.services.IndexerService;
import org.streets.extention.lucene.services.SearcherService;
import org.streets.extention.lucene.services.impl.IndexSourceImpl;
import org.streets.extention.lucene.services.impl.IndexerServiceImpl;
import org.streets.extention.lucene.services.impl.SearcherServiceImpl;

/**
 * @version $Id: ChenilleKitLuceneModule.java 356 2008-11-25 12:49:51Z homburgs $
 */
public class LuceneModule
{
	/**
	 *
	 * @param logger
	 * @param configurationMap
	 * @param shutdownHub
	 * @return
	 */
	public static IndexSource buildIndexSource(Logger logger, Map<String, Resource> configurationMap,
						RegistryShutdownHub shutdownHub)
	{
		Resource config = configurationMap.get(LuceneConstants.CONFIG_KEY_PROPERTIES);
		IndexSourceImpl service = new IndexSourceImpl(logger, config);

		shutdownHub.addRegistryShutdownListener(service);

		return service;
	}

    /**
     * bind the <a href="http://lucene.apache.org/java/docs/index.html">lucene</a> based indexer service.
     *
     * @param logger        system logger
     * @param soruce {@link IndexSource} service
     * @param threadManager the thread manager
     *
     * @return indexer engine
     */
    @Scope(ScopeConstants.PERTHREAD)
    public static IndexerService buildIndexerService(Logger logger, IndexSource source,
    						PerthreadManager threadManager)
    {
        IndexerServiceImpl service = new IndexerServiceImpl(logger, source);
        threadManager.addThreadCleanupListener(service);
        return service;
    }

    /**
     * bind the <a href="http://lucene.apache.org/java/docs/index.html">lucene</a> based searcher service.
     *
     * @param logger        system logger
     * @param configuration IOC configuration map
     * @param threadManager	Manager services to clean up resources
     *
     * @return searcher engine
     */
    public static SearcherService buildSearcherService(Logger logger, IndexSource source,
                                                       PerthreadManager threadManager)
    {
        SearcherServiceImpl service = new SearcherServiceImpl(logger, source);

        threadManager.addThreadCleanupListener(service);

        return service;
    }
}
